package com.transportation.app.bean;
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.core.Observable;
import io.reactivex.rxjava3.disposables.CompositeDisposable;
import io.reactivex.rxjava3.observers.DisposableObserver;
import io.reactivex.rxjava3.schedulers.Schedulers;

/**
 * @ClassName BaseViewModel
 * TODO:基础请求封装
 * @Date 2023/6/29 10:28
 * @Author wei ang
 */
public class BaseViewModel {

    //请求订阅管理器
    private CompositeDisposable compositeDisposable;

    /**
     * TODO: 绑定订阅
     * @return void
     * @time 2023/6/29 10:29
     * @author w.a
     */
    @SuppressWarnings("unchecked")
    public void addDisposable(Observable<?> observable, DisposableObserver observer) {
        if (compositeDisposable == null) {
            compositeDisposable = new CompositeDisposable();
        }
        compositeDisposable.add(observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeWith(observer));
    }

    /**
     * TODO: 移除订阅
     * @return void
     * @time 2023/6/29 10:29
     * @author w.a
     */
    public void removeDisposable() {
        if (compositeDisposable != null) {
            compositeDisposable.dispose();
        }
    }

}

